﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Runtime.Serialization.Json;
namespace HHCommon
{
    public class Help
    {
        #region //获取页面请求方式：GET和POST
        /// <summary>
        /// 获取页面请求方式：GET和POST
        /// </summary>
        /// <returns></returns>
        public string GetHttpMethod()
        {
            return HttpContext.Current.Request.HttpMethod;
        }
        #endregion

        #region //判断页面请求方式是GET还是POST
        /// <summary>
        ///判断页面请求方式是GET还是POST
        /// </summary>
        /// <returns>布尔值</returns>
        public bool GetHttpMethodBool()
        {
            if (HttpContext.Current.Request.HttpMethod == "POST")
                return true;
            else
                return false;
        }
        #endregion

        #region //设置cookie值
        /// <summary>
        /// 设置cookie值
        /// </summary>
        /// <param name="KeyName">Cookie-key键名称</param>
        /// <param name="Value">Cookie值</param>
        ///<param name="SCT">设置过期时间的一个枚举类型的数据</param>
        ///<param name="i">设置过期时间</param>
        public void SetCookie(string KeyName, string Value,SetCookieTime SCT, int i)
        {
            HttpCookie cookie = new HttpCookie("YFZ_"+KeyName);
            cookie[KeyName] = Value;
            if (i!= 0)
            {
                switch (SCT)
                {
                    case SetCookieTime.Year:
                        cookie.Expires = DateTime.Now.AddYears(i);
                        break;
                    case SetCookieTime.Month:
                        cookie.Expires = DateTime.Now.AddMonths(i);
                        break;
                    case SetCookieTime.Day:
                        cookie.Expires = DateTime.Now.AddDays(i);
                        break;
                    case SetCookieTime.Hours:
                        cookie.Expires = DateTime.Now.AddHours(i);
                        break;
                    case SetCookieTime.Minutes:
                        cookie.Expires = DateTime.Now.AddMinutes(i);
                        break;
                    case SetCookieTime.Seconds:
                        cookie.Expires = DateTime.Now.AddSeconds(i);
                        break;
                }
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
             
        #endregion

        #region //获取cookie值
        /// <summary>
        /// 获取cookie值
        /// </summary>
        /// <param name="KeyName">Cookie-key键名称</param>
        /// <returns></returns>
        public string GetCookie(string KeyName)
        {
            string Cookie=string.Empty;
            try
            {
                HttpCookie Cookies = HttpContext.Current.Request.Cookies.Get("YFZ_"+KeyName);
                if (!string.IsNullOrEmpty(Cookies[KeyName].ToString()))
                    Cookie = Cookies[KeyName].ToString();
            }
            catch
            {
            }
            return Cookie;
        }
        #endregion

        #region //清空cookie值
        /// <summary>
        /// 清空cookie值
        /// </summary>
        /// <param name="KeyName">Cookie-key键名称</param>
        /// <param name="SCT">设置过期时间的一个枚举类型的数据</param>
        /// <param name="i">设置过期时间</param>
        public void EmptyCookie(string KeyName, SetCookieTime SCT, int i)
        {
            try
            {
                HttpCookie Cookies = HttpContext.Current.Request.Cookies.Get("YFZ_" + KeyName);
                Cookies[KeyName] = "";
                switch (SCT)
                {
                    case SetCookieTime.Year:
                        Cookies.Expires = DateTime.Now.AddYears(-i - 1);
                        break;
                    case SetCookieTime.Month:
                        Cookies.Expires = DateTime.Now.AddMonths(-i - 12);
                        break;
                    case SetCookieTime.Day:
                        Cookies.Expires = DateTime.Now.AddDays(-i - 30);
                        break;
                    case SetCookieTime.Hours:
                        Cookies.Expires = DateTime.Now.AddHours(-i - 24);
                        break;
                    case SetCookieTime.Minutes:
                        Cookies.Expires = DateTime.Now.AddMinutes(-i - 3600);
                        break;
                    case SetCookieTime.Seconds:
                        Cookies.Expires = DateTime.Now.AddSeconds(-i - 36000);
                        break;
                }
                HttpContext.Current.Response.Cookies.Add(Cookies);
            }
            catch { }
        }
        #endregion

        #region //获取页面名称方法
        /// <summary>
        /// 获取页面名称方法
        /// </summary>
        /// <returns></returns>
        public string GetPageName()
        {
            return HttpContext.Current.Request.Url.AbsolutePath;
        }
        #endregion

        #region //获取页面参数方法
        /// <summary>
        /// 获取页面参数方法
        /// </summary>
        /// <returns></returns>
        public string GetPageParameter()
        {
            return HttpContext.Current.Request.Url.Query;;
        }
        #endregion

        #region //判断页面是否带参数
        /// <summary>
        /// 判断页面是否带参数
        /// </summary>
        /// <returns></returns>
        public bool IFPageParameter()
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Url.Query))
                return true;
            else
                return false;
        }
        #endregion

        #region //随机编号
        public string GetRandom()
        {
            Random rd = new Random();
            return DateTime.Now.ToString("yyyyMMddHHmmss")+rd.Next(1, 10000).ToString();
        }

        #endregion

        #region //MD5加密算法
        /// <summary>
        /// MD5加密算法
        /// </summary>
        /// <param name="password">明文</param>
        /// <returns>MD5密文</returns>
        public string GetMD5(string password)
        {
            return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
        }
        #endregion

        #region //设置缓存Cache
        /// <summary>
        /// //设置缓存Cache
        /// </summary>
        /// <param name="Obj">被缓存对象</param>
        /// <param name="i">设置缓存时间</param>
        public void CreateCache(string KeyWord, object Obj,int i)
        {
            try
            {
                if (System.Web.HttpContext.Current.Cache[KeyWord] == null)
                {
                    //数据缓存
                    System.Web.HttpContext.Current.Cache.Insert(KeyWord,Obj, null, DateTime.Now.AddMinutes(i), TimeSpan.Zero);
                }
            }
            catch
            {}
        }
        #endregion

        #region //字符串截取
        /// <summary>
        /// 字符串截取
        /// </summary>
        /// <param name="size">截取长度</param>
        /// <param name="Str">被截取的字符串</param>
        /// <param name="ReplaceStr">替换字符串</param>
        /// <returns></returns>
        public static string CutOut(int size, string Str, string ReplaceStr)
        {
            if (Str.Length > size)
                Str = Str.Substring(0,size)+ReplaceStr;
            return Str;
        }
        #endregion

        #region //序列化json
        /// <summary>
        /// 序列化json
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Serialize<T>(T data)
        {
            using (var memoryStream = new MemoryStream())
            {
                var serializer = new DataContractJsonSerializer(typeof(T));
                serializer.WriteObject(memoryStream, data);

                memoryStream.Seek(0, SeekOrigin.Begin);

                var reader = new StreamReader(memoryStream);
                string content = reader.ReadToEnd();
                return content;
            }
        }
        #endregion

        #region //把json格式反序列化
        /// <summary>
        /// 把json格式反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public T Deserialize<T>(string jsonString)
        {
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(jsonString)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

                return (T)serializer.ReadObject(ms);
            }
        }
        #endregion
    }

    #region //时间设置枚举
    public enum SetCookieTime
    {
        Year,
        Month,
        Day,
        Hours,
        Minutes,
        Seconds
    }
    #endregion
}
